package com.jbp.controller;

import com.jbp.model.BankUser;
import com.jbp.service.BankUserService;
import com.jbp.utils.PageUtils;
import com.jbp.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * 提现银行(BankUser)表控制层
 * @author cc
 * @since 2021-06-28 16:50:40
 */
@Api(tags ="APP接口--BankUser" )
@RestController
@RequestMapping("rest/bankUser")
public class BankUserController {
    @Autowired
    private BankUserService bankUserService;
    
    @ApiOperation(value="信息")
    @GetMapping("/info")
    public R info(@RequestAttribute("userId")Integer userId){
        BankUser bankUser = bankUserService.getByUserId(userId);
        return R.ok(bankUser);
    }
    
    /** 保存 */
    @PostMapping("/save")
    public R save(@RequestBody BankUser bankUser,@RequestAttribute("userId")Integer userId){
        if(StringUtils.isBlank(bankUser.getBankName())) {
            return R.error("请选择银行");
        }
        if(StringUtils.isBlank(bankUser.getBranch())) {
            return R.error("请填写开户支行");
        }
        if(StringUtils.isBlank(bankUser.getCardNum())) {
            return R.error("请填写卡号");
        }
        if(StringUtils.isBlank(bankUser.getMobile())) {
            return R.error("请填写预留手机号");
        }
        BankUser result = bankUserService.getByUserId(userId);
        if (result!=null) {
            return R.error("已绑定过了");
        }
        bankUser.setUserId(userId);
        bankUserService.save(bankUser);
        return R.ok();
    }

    /** 修改 */
    @PostMapping("/update")
    public R update(@RequestBody BankUser bankUser, @RequestAttribute("userId")Integer userId){
        if(StringUtils.isBlank(bankUser.getBankName())) {
            return R.error("请选择银行");
        }
        if(StringUtils.isBlank(bankUser.getBranch())) {
            return R.error("请填写开户支行");
        }
        if(StringUtils.isBlank(bankUser.getCardNum())) {
            return R.error("请填写卡号");
        }
        if(StringUtils.isBlank(bankUser.getMobile())) {
            return R.error("请填写预留手机号");
        }
        BankUser result = bankUserService.getByUserId(userId);
        if (result==null) {
            return R.error("请先绑定银行卡");
        }
        result.setBankName(bankUser.getBankName());
        result.setBranch(bankUser.getBranch());
        result.setCardNum(bankUser.getCardNum());
        result.setMobile(bankUser.getMobile());
        bankUserService.updateById(result);
        return R.ok();
    }

}